package cz.acies.organizer.data.persist;

import java.io.Serializable;

public class PostalAddress implements Serializable
{
	private static final long serialVersionUID = 5264378282575806934L;
	
	public static final int REL_HOME = 0;
	public static final int REL_WORK = 1;
	public static final int REL_OTHER = 2;

	private String street = "";
	private String city = "";
	private String region = "";
	private String postCode = "";
	private String country = "";
	//private boolean primary = false;
	//private int rel = REL_HOME;
	
	public PostalAddress () {}
	
	public PostalAddress (String street, String city, String state, String postCode, String country) {
		this.street = (street != null) ? street : "";
		this.city = (city != null) ? city : "";
		this.region = (state != null) ? state : "";
		this.postCode = (postCode != null) ? postCode : "";
		this.country = (country != null) ? country : "";
	}
	
	public void parseText(String text) {
		String[] result = text.split(";");
		if (result.length > 2) {
			street = result[2];
		}
		if (result.length > 3) {
			city = result[3];
		}
		if (result.length > 4) {
			region = result[4];
		}
		if (result.length > 5) {
			postCode = result[5];
		}
		if (result.length > 6) {
			country = result[6];
		}
	}
	
	public String toString() {
		//;;street;city;region;postCode;country;
		return ";;" + street + ";" + city + ";" + region  + ";" + postCode  + ";" + country  + ";";
	}
	
	public boolean isEmpty() {
		boolean result = true;
		if (hasStreet()) return false;
		if (hasCity()) return false;
		if (hasPostCode()) return false;
		if (hasRegion()) return false;
		return result;
		
	}
	
	// -----  -------------------------------------------------------------
	
	public String getStreet() {
		return street;
	}

	public void setStreet(String street) {
		this.street = street;
	}

	public boolean hasStreet() {
		return (street != null && street.trim().length() > 0);
	}

	public String getCity() {
		return city;
	}

	public void setCity(String city) {
		this.city = city;
	}

	public boolean hasCity() {
		return (city != null && city.trim().length() > 0);
	}

	public String getPostCode() {
		return postCode;
	}

	public void setPostCode(String postCode) {
		this.postCode = postCode;
	}

	public boolean hasPostCode() {
		return (postCode != null && postCode.trim().length() > 0);
	}

	public String getRegion() {
		return region;
	}

	public void setRegion(String region) {
		this.region = region;
	}

	public boolean hasRegion() {
		return (region != null && region.trim().length() > 0);
	}

	public String getCountry() {
		return country;
	}

	public void setCountry(String country) {
		this.country = country;
	}
	
	// -----  -----------------------------------------------------------------
	
	public static PostalAddress parse(String value) {
		String[] result = value.split(";");
		PostalAddress address = new PostalAddress();
		if (result.length > 2) {
			address.setStreet(result[2]);
		}
		if (result.length > 3) {
			address.setCity(result[3]);
		}
		if (result.length > 4) {
			address.setRegion(result[4]);
		}
		if (result.length > 5) {
			address.setPostCode(result[5]);
		}
		if (result.length > 6) {
			address.setCountry(result[6]);
		}
		return address;
	}
}
